﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.NetworkRemoteDevice;
using Domain.Entities;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace Web.Controllers
{
    public class SurveyController : Controller
    {
        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }
        public ActionResult CommonSurvey()
        {
            ViewBag.Names = RemoteDeviceManager.GetNamesOfRemoteDevices();
            return View();
        }
        public ActionResult ErrorSurvey()
        {
            var dbWorker = new DBWorker();
            List<ErrorLogs> lstEL = dbWorker.getErrors();

            return View("ErrorSurvey", lstEL);
        }
        public ActionResult SurveyDetailed(string devName, double daysAgo = -0.2)
        {
            if (!RemoteDeviceManager.hasDevice(devName))
            {
                return View("ErrorDeviceNotFound");
            }
            if (daysAgo > 0)
            {
                daysAgo *= -1;
            }
            if (daysAgo == 0) daysAgo = -0.2;
            
            string scriptStart = "<script type=\"text/javascript\"> window.onload = function () { var chart = new CanvasJS.Chart(\"chartContainer\", {zoomEnabled: true, title: {text: \"ICMP Echo-Reply\",fontFamily: \"courier\",fontSize: 12,fontWeight: \"lighter\",margin: 10,padding: 5,verticalAlign: \"top\",horizontalAlign: \"left\",},axisX:{ valueFormatString: \"DD.MM.YY HH:mm:ss\",labelAngle: -50},axisY: {title: \"Запрос-ответ, мс\"},data:[{type: \"scatter\",dataPoints:[";
            string scriptEnd = "] }] }); chart.render(); } </script>";
            ViewBag.devName = devName;
            StringBuilder sb = new StringBuilder();
            sb.Append(scriptStart);
            DBWorker dbWorker = new DBWorker();
            List<DataGraphic> dataGraphic = dbWorker.getEchoResultInDateRange(devName, DateTime.Now.AddDays(daysAgo), DateTime.Now);
            int[] minMaxAvg = dbWorker.getMinMaxAvg(devName, DateTime.Now.AddDays(daysAgo), DateTime.Now);
            foreach (var item in dataGraphic)
            {
                sb.AppendFormat("{{ x: new Date({0}, {1}, {2}, {3}, {4}, {5}), y: {6}{7}}},",
                                                                             item.year, //0
                                                                             item.month-1, //1
                                                                             item.day, //2
                                                                             item.hour, //3
                                                                             item.minute, //4
                                                                             item.second, //5
                                                                             item.result, //6
                                                                             item.result==-20?" , indexLabel: \"\",markerColor: \"DarkSlateGrey\", markerType: \"cross\"":""
                    );
            }
            sb.Append(scriptEnd);
            ViewBag.EchoGraphicResult = sb.ToString();
            ViewBag.MinMaxAvg = minMaxAvg;
            ViewBag.lstErrors = dbWorker.getErrorsOfDevice(devName, daysAgo);
            return View("SurveyDetailed");
        }
    }
}
